#include <iostream>
#include <unordered_map>
#include <string>
#include <cstdlib> // 添加system调用需要的头文件

using namespace std;

// 定义游戏状态
enum class GameState
{
    START,         // 起始场景（古堡入口）
    HALLWAY,       // 核心中转场景（大厅）
    ARMORY,        // 装备获取场景（武器库）
    TREASURE_ROOM, // 战斗场景（宝藏房）
    DRAGON_LAIR,   // 最终挑战场景（龙穴）
    WIN,           // 胜利状态
    GAME_OVER      // 失败状态
};

// 游戏状态描述
unordered_map<GameState, string> stateDescriptions = {
    {GameState::START, "你站在古堡入口，面前有一扇沉重的木门（输入1进入）"},
    {GameState::HALLWAY, "你进入了一个长长的大厅，左右各有一扇门（左2/右3）后面还有通道（4）"},
    {GameState::ARMORY, "这里是武器库，墙上挂着生锈的剑（拿剑输入1，返回输入2）"},
    {GameState::TREASURE_ROOM, "你发现了宝藏房间！但守卫出现了！（战斗1/逃跑2）"},
    {GameState::DRAGON_LAIR, "岩浆池边沉睡着一头巨龙！（悄悄通过1/攻击2）"},
    {GameState::WIN, "你成功获得了宝藏！游戏胜利！"},
    {GameState::GAME_OVER, "游戏结束！"}};

class GameFA
{
private:
    GameState currentState;
    bool hasSword;
    // 状态转移规则
    unordered_map<GameState, unordered_map<int, GameState>> transitions = {
        {GameState::START, {{1, GameState::HALLWAY}}},
        {GameState::HALLWAY, {{2, GameState::ARMORY},
         {3, GameState::TREASURE_ROOM}, {4, GameState::DRAGON_LAIR}}},
        {GameState::ARMORY, {{1, GameState::ARMORY}, {2, GameState::HALLWAY}}},
        {GameState::TREASURE_ROOM, {{1, GameState::WIN}, {2, GameState::HALLWAY}}},
        {GameState::DRAGON_LAIR, {{1, GameState::WIN}, {2, GameState::GAME_OVER}}}};

public:
    GameFA() : currentState(GameState::START), hasSword(false) {}

    void start()
    {
        while (currentState != GameState::WIN && currentState != GameState::GAME_OVER)
        {
            displayState();
            processInput();
        }
        displayState();
    }

private:
    void displayState()
    {
        // 清屏（Windows）
        cout << stateDescriptions[currentState] << "\n";
    }

    void processInput()
    {
        int choice;
        cout << "请选择操作：";
        cin >> choice;

        // 特殊状态处理
        switch (currentState)
        {
        case GameState::ARMORY:
            if (choice == 1)
            {
                hasSword = true;
                cout << "你拿起了生锈的剑！\n";
 
            }
            break;
        case GameState::TREASURE_ROOM:
            if (choice == 1)
            {
                if (hasSword)
                {
                    cout << "你用剑击败了守卫！\n";
                }
                else
                {
                    cout << "你被守卫击败了！\n";
                    currentState = GameState::GAME_OVER;
                    return;
                }
       
            }
            break;
        case GameState::DRAGON_LAIR:
            if (choice == 2 && !hasSword)
            {
                cout << "没有武器攻击巨龙是自杀行为！\n";
                currentState = GameState::GAME_OVER;
                return;
            }
            if (choice == 2 && hasSword)
            {
                cout << "武器攻击巨龙！\n";
                currentState = GameState::WIN;
                return;
            }
            
            break;
        }

        // 状态转移
        if (transitions[currentState].count(choice))//.count(choice) 是 STL 容器（如 std::map）的方法，返回键（choice）存在的次数（0 或 1）。
        {
            currentState = transitions[currentState][choice];//更新当前状态
        }
        else
        {
            cout << "无效输入！\n";
            system("pause");
        }
    }
};

int main()
{
    GameFA game;
    game.start();
    return 0;
}